<template>
  <div class="dialog">
    <el-dialog title="疾病处理详情" :visible.sync="showDialog" width="65%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-row v-for="(item, index) in rightTableData" :key="index + 'rightTableData'">
        <div class="recordTitle">记录{{ index + 1 }}</div>
        <el-form class="el-form">
          <el-row :gutter="15">
            <el-col :span="7">
              <el-form-item label="水温： " class="w100p">
                <el-input v-model="item.waterTemperature" type="number" clearable placeholder>
                  <template slot="append">°C</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="处理人： " class="w100p">
                <!-- <el-input v-model="item.staffName" clearable placeholder /> -->
                <el-select filterable clearable v-model="item.staffName" placeholder="请选择">
                  <el-option v-for="(item, index) in staffList" :label="item.staffName" :value="item.staffName"
                    :key="index + 'staff'" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="处理日期： " class="w100p">
                <el-date-picker v-model="item.dealDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                  clearable />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="" class="w100p">
                <el-button type="text" style="color:red;text-decoration: underline;" size="small"
                  @click.native.prevent="editRow(item)">提交修改</el-button>
                <el-button type="text" style="color:red;text-decoration: underline;height: 36px;" size="small"
                  @click.native.prevent="deleteRow(item)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-table ref="rightTableData" :data="item.diseaseDealDetailList" highlight-current-row border stripe>
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column label="药品名称" prop="materialName">
              <template slot-scope="scope">
                <el-select filterable clearable v-model="scope.row.materialId">
                  <el-option v-for="item of materialList" :key="item.materialId + 'materialId'" :label="item.name"
                    :value="item.materialId" />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="用药量" prop="amount">
              <template slot-scope="scope">
                <el-input class="w50p" type="number" v-model="scope.row.amount" @change="() => {
                  if (scope.row.amount < 0) { scope.row.amount = ''; return }
                  scope.row.num = scope.row.amount / materialObj[scope.row.materialId].ratioNum
                  $set(item.diseaseDealDetailList, scope.$index, {
                    materialId: scope.row.materialId,
                    num: scope.row.num,
                    amount: scope.row.amount
                  })
                }" />
                <span>{{ scope.row.materialId ? materialObj[scope.row.materialId].unit : '' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="数量" prop="amount">
              <template slot-scope="scope">
                <el-input class="w50p" type="number" v-model="scope.row.num" @change="() => {
                  if (scope.row.num < 0) { scope.row.num = ''; return }
                  scope.row.amount = scope.row.num * materialObj[scope.row.materialId].ratioNum
                  $set(item.diseaseDealDetailList, scope.$index, {
                    materialId: scope.row.materialId,
                    num: scope.row.num,
                    amount: scope.row.amount
                  })
                }" />
                <span>{{ scope.row.materialId ? materialObj[scope.row.materialId].aUnit : '' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="药品用法" prop="usage">
              <template slot-scope="scope">
                <el-input v-model="scope.row.usage" />
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scoped">
                <el-button type="text" style="color:red;text-decoration: underline;"
                  @click.native.prevent="editRecordRow(scoped.row)" size="small">提交修改</el-button>
                <el-button type="text" style="color:red;text-decoration: underline;" size="small"
                  @click.native.prevent="deleteRecordRow(scoped.row)">删除</el-button>
              </template>
            </el-table-column>
            <template slot="empty">
              <div style="color:#000">暂无数据</div>
            </template>
          </el-table>
        </el-form>
        <!-- <Pagination
          :page-size="5"
          :current-page="currentPage"
          :total-size="totalSize"
          @changeCurrentPage="changeCurrentPage"
        /> -->
      </el-row>
      <div v-show="rightTableData.length == 0" style="text-align: center;">暂无数据</div>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div> -->
    </el-dialog>

    <DealDialog ref="addEditDialog" :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog"
      @cancelDialog="cancel" />
  </div>
</template>

<script>
import SpecificationApi from "@/api/basic-info-manage/specification"
import Pagination from "@/components/Pagination";
import DealDialog from "./deal-dialog.vue";
import { mapGetters } from "vuex";
import { mymixin } from "@/utils/mixin";

import HealthTaskHandlingApi from "@/api/health-management/health-task-handling";
import StaffApi from "@/api/basic-info-manage/staff";
import MaterialApi from "@/api/basic-info-manage/material";
import store from "@/store";
export default {
  name: "",
  components: { Pagination, DealDialog },
  mixins: [mymixin],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    completeInfoObject: {
      type: [Object],
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      rightTableData: [],
      specificationList: [],
      currentBatchId: 1,
      showDialog: this.show,
      showAddEditDialog: false,
      staffList: [],
      materialList: []
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),

    materialObj() {
      return store.state.basicData.material;
    }
  },
  watch: {
    show(v) {
      this.showDialog = v;
      console.log(this.completeInfoObject)
    },
    completeInfoObject(v) {
      this.queryInputPondDetail(v)
      this.querySpecification(v)
    },
  },
  created() { },
  mounted() {
    this.queryCondition()
  },
  methods: {
    queryCondition() {
      StaffApi.queryByCondition().then(res => {
        this.staffList = res.data
      })
      MaterialApi.queryByCondition().then(res => {
        this.materialList = res.data;
      })


    },
    queryPageList() {
      this.queryInputPondDetail(this.completeInfoObject)
    },
    queryInputPondDetail(v) {
      let params = {
        diseaseRecordId: v.diseaseRecordId
      }
      HealthTaskHandlingApi.queryByDiseaseRecordId(params).then(res => {
        if (res.success) {
          this.totalSize = res.data.totalElements;
          this.rightTableData = res.data;
          this.rightTableData.forEach(info => {
            console.log(info)
            info.diseaseDealDetailList.forEach((data, index) => {
              console.log(data)
              this.$set(info.diseaseDealDetailList, index, {
                ...data,
                ...{
                  materialId: data.materialId,
                  num: data.amount / this.materialObj[data.materialId].ratioNum,
                  amount: data.amount
                }
              })
            })
          })
        }
      })
    },
    // 查询品种的规格
    querySpecification(v) {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          fishVarietyId: v.varietyId
        }
      }
      SpecificationApi.queryPageByCondition(params).then(res => {
        if (res.success) {
          this.specificationList = res.data.content
        }
      })
    },
    // 修改一行数据
    editRow(item) {
      // this.$emit('editRecord', item)
      this.$confirm("确定提交修改吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        HealthTaskHandlingApi.edit(item).then(res => {
          if (res.success) {
            this.$message.success("处理成功");
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },
    // 删除一行数据
    deleteRow(rows) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        let params = {
          id: rows.diseaseDealId,
        }
        HealthTaskHandlingApi.delById(params).then(res => {
          if (res.success === true) {
            this.$message.success("删除成功！");
            this.queryPageList()
          } else {
            this.$message.error(res.message);
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    // 修改一行药品数据
    editRecordRow(row) {
      if (!row.amount) {
        this.$message.warning('请输入药品用量');
        return
      }
      this.$confirm("确定提交修改吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        HealthTaskHandlingApi.editDetail(row).then(res => {
          if (res.success) {
            this.$message.success("处理成功");
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },
    // 删除一行药品数据
    deleteRecordRow(rows) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        let params = {
          id: rows.diseaseDealDetailId,
        }
        HealthTaskHandlingApi.delByIdDetail(params).then(res => {
          if (res.success === true) {
            this.$message.success("删除成功！");
            this.queryPageList()
          } else {
            this.$message.error(res.message);
          }
        })
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    },
  },
};
</script>

<style lang="scss" scoped>
.pagination {
  position: absolute;
  right: 0px;
  bottom: -40px;
}

.el-table {
  position: relative;
}

::v-deep .el-dialog__body {
  padding: 30px 20px 50px 20px;

  .recordTitle {
    font-size: 18px;
    color: #237FC3;
    text-align: center;
    margin: 10px 0 -10px;
  }
}

::v-deep .el-form {
  display: flex;
  flex-wrap: wrap;
  padding: 2px;
  width: 100%;

  .el-form-item__label {
    color: #000;
    white-space: nowrap;
    font-size: 14px;
    min-width: 80px;
    text-align: end;
  }
}

::v-deep .el-form-item--medium {
  display: flex;
  margin-bottom: 0;
  padding: 10px;

  .el-form-item__content {
    display: flex;
    align-items: center;
    width: 100%;
    line-height: 36px;

    >span {
      white-space: nowrap;
      min-width: 50px;
      text-align: end;
      color: #000;
    }

    .el-input {
      width: 100%;
    }
  }

  .el-form-item__label {
    line-height: 36px;
  }
}

::v-deep {
  .el-row {
    margin: 20px 0;
    // padding-bottom: 20px;
    width: 100%;
    border: #000 1px solid;

    // border-bottom-width: 0;
    // &:last-child {
    //   border-bottom-width: 1px;
    // }
    .el-col {
      border: #000 1px solid;
      border-bottom-width: 1;
      border-right-width: 0;
      border-top-width: 0;

      &:first-child {
        border-left-width: 0;
      }
    }
  }
}
</style>
